import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/module/color.dart';
import 'package:sui_shou_pai/module/effect.dart';
import 'package:sui_shou_pai/my/init_info/init_information.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';

class BottomToolBar extends StatefulWidget {
  final String currentContentType;
  final String currentContentId;
  final Function callback;

  BottomToolBar(
      {Key key, this.currentContentType, this.currentContentId, this.callback})
      : super(key: key);

  @override
  _BottomToolBarState createState() => _BottomToolBarState();
}

class _BottomToolBarState extends State<BottomToolBar> {
  bool _isCommenting = false;
  bool _isLiked = false;
  var _controller;
  FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _controller.addListener(() {});
    judgeLike();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 60,
      padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0),
      decoration:
          BoxDecoration(boxShadow: [shadowNoOffset()], color: Colors.white),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
                margin: EdgeInsets.only(right: 6),
                child: ClipRRect(
                  borderRadius: borderRadiusAll4(),
                  child: TextField(
                    onTap: () {
                      setState(() {
                        _isCommenting = true;
                      });
                    },
                    controller: _controller,
                    focusNode: _focusNode,
                    style: TextStyle(
                      fontSize: 16,
                    ),
                    decoration: InputDecoration(
                        isCollapsed: true,
                        contentPadding:
                            EdgeInsets.symmetric(vertical: 8, horizontal: 12),
                        fillColor: greyF3F3F3,
                        filled: true,
                        border: InputBorder.none,
                        hintText: '说亿点好听的'),
                    cursorColor: Colors.grey,
                    maxLines: 3,
                    minLines: 1,
                  ),
                )),
          ),
          initSideTool()
        ],
      ),
    );
  }

  Widget initSideTool() {
    if (_isCommenting) {
      return GestureDetector(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
          decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: borderRadiusAll4(),
          ),
          child: Row(
            children: <Widget>[
              Icon(
                Icons.send,
                color: Colors.white,
                size: 16,
              ),
              Text(
                ' 提交',
                style: TextStyle(color: Colors.white, fontSize: 16),
              )
            ],
          ),
        ),
        onTap: () {
          addComment(_controller.text);
          Fluttertoast.showToast(
            msg: "评论成功",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
          );
          setState(() {
            _isCommenting = false;
          });
          _focusNode.unfocus();
          _controller.clear();
        },
      );
    } else {
      return Row(
        children: <Widget>[
          widget.currentContentType == "announcement"
              ? SizedBox.shrink()
              : likeIconButton(),
          IconButton(
              onPressed: () {}, icon: Icon(Icons.share, color: Colors.grey)),
        ],
      );
    }
  }

  Widget likeIconButton() {
    if (_isLiked == true) {
      return IconButton(
          onPressed: () {
            deleteLike();
          },
          icon: Icon(Icons.thumb_up_alt_sharp, color: Colors.redAccent));
    } else {
      return IconButton(
          onPressed: () {
            addLike();
          },
          icon: Icon(Icons.thumb_up_outlined, color: Colors.grey));
    }
  }

  Future<void> addComment(String content) async {
    if (widget.currentContentType == "news") {
      HttpRequest.request("$BASE_URL/news-comment/addNewsComment",
              method: "POST",
              params: {
                "newsId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
                "content": content
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {
        widget.callback(true);
      });
    } else if (widget.currentContentType == "announcement") {
      HttpRequest.request(
              "$BASE_URL/announcement-comment/addAnnouncementComment",
              method: "POST",
              params: {
                "announcementId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
                "content": content
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {
        widget.callback(true);
      });
    } else if (widget.currentContentType == "record") {
      HttpRequest.request(
              "$BASE_URL/photo-record-comment/addPhotoRecordComment",
              method: "POST",
              params: {
                "photoRecordId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
                "content": content
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {});
    }
  }

  Future<void> judgeLike() async {
    if (widget.currentContentType == "news") {
      HttpRequest.request("$BASE_URL/news-like/isNewsLiked",
              method: "GET",
              params: {
                "newsId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {
        setState(() {
          _isLiked = value.data["result"] > 0 ? true : false;
        });
      });
    } else if (widget.currentContentType == "record") {
      HttpRequest.request("$BASE_URL/photo-record-like/isPhotoRecordLiked",
              method: "GET",
              params: {
                "photoRecordId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {
        setState(() {
          _isLiked = value.data["result"] > 0 ? true : false;
        });
      });
    }
  }

  Future<void> addLike() async {
    if (widget.currentContentType == "news") {
      HttpRequest.request("$BASE_URL/news-like/addNewsLike",
              method: "POST",
              params: {
                "newsId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {});
      setState(() {
        _isLiked = true;
      });
    } else if (widget.currentContentType == "record") {
      HttpRequest.request("$BASE_URL/photo-record-like/addPhotoRecordLike",
              method: "POST",
              params: {
                "photoRecordId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {});
      setState(() {
        _isLiked = true;
      });
    }
  }

  Future<void> deleteLike() async {
    if (widget.currentContentType == "news") {
      HttpRequest.request("$BASE_URL/news-like/deleteNewsLike",
              method: "DELETE",
              params: {
                "newsId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {});
      setState(() {
        _isLiked = false;
      });
    } else if (widget.currentContentType == "record") {
      HttpRequest.request("$BASE_URL/photo-record-like/deletePhotoRecordLike",
              method: "DELETE",
              params: {
                "photoRecordId": widget.currentContentId,
                "employeeId": InitInformation.employeeId,
              },
              token: await StorageUtil.getStringItem("token"))
          .then((value) {});
      setState(() {
        _isLiked = false;
      });
    }
  }
}
